\hypertarget{a00117}{}\section{事件循环}
\label{a00117}\index{事件循环@{事件循环}}


网络事件循环  


\subsection*{函数}
\begin{DoxyCompactItemize}
\item 
\hyperlink{a00056_a97fc76209a58362019f1ded9169e397f_a97fc76209a58362019f1ded9169e397f}{kloop\+\_\+t} $\ast$ \hyperlink{a00117_ga2e4181ebe9c1769be90bb8dc0e5cdffe_ga2e4181ebe9c1769be90bb8dc0e5cdffe}{knet\+\_\+loop\+\_\+create} ()
\begin{DoxyCompactList}\small\item\em 创建一个事件循环 \end{DoxyCompactList}\item 
void \hyperlink{a00117_ga390ea3161f935d0d3a411df8752d8b9f_ga390ea3161f935d0d3a411df8752d8b9f}{knet\+\_\+loop\+\_\+destroy} (\hyperlink{a00056_a97fc76209a58362019f1ded9169e397f_a97fc76209a58362019f1ded9169e397f}{kloop\+\_\+t} $\ast$loop)
\begin{DoxyCompactList}\small\item\em 销毁事件循环 事件循环内的所有管道也会被销毁 \end{DoxyCompactList}\item 
\hyperlink{a00056_a3b7e82599367eade261456f60ebe2cd9_a3b7e82599367eade261456f60ebe2cd9}{kchannel\+\_\+ref\+\_\+t} $\ast$ \hyperlink{a00117_gac1f9a4848c06c2a6a2723d7b991b4394_gac1f9a4848c06c2a6a2723d7b991b4394}{knet\+\_\+loop\+\_\+create\+\_\+channel} (\hyperlink{a00056_a97fc76209a58362019f1ded9169e397f_a97fc76209a58362019f1ded9169e397f}{kloop\+\_\+t} $\ast$loop, uint32\+\_\+t max\+\_\+send\+\_\+list\+\_\+len, uint32\+\_\+t recv\+\_\+ring\+\_\+len)
\begin{DoxyCompactList}\small\item\em 创建管道 \end{DoxyCompactList}\item 
\hyperlink{a00056_a3b7e82599367eade261456f60ebe2cd9_a3b7e82599367eade261456f60ebe2cd9}{kchannel\+\_\+ref\+\_\+t} $\ast$ \hyperlink{a00117_gaddfd5a2709d0c26ada8a2dd3fd6a5b62_gaddfd5a2709d0c26ada8a2dd3fd6a5b62}{knet\+\_\+loop\+\_\+create\+\_\+channel\+\_\+exist\+\_\+socket\+\_\+fd} (\hyperlink{a00056_a97fc76209a58362019f1ded9169e397f_a97fc76209a58362019f1ded9169e397f}{kloop\+\_\+t} $\ast$loop, \hyperlink{a00056_a0d9e0afbf02fb6ed6c5b1415dce51b05_a0d9e0afbf02fb6ed6c5b1415dce51b05}{socket\+\_\+t} socket\+\_\+fd, uint32\+\_\+t max\+\_\+send\+\_\+list\+\_\+len, uint32\+\_\+t recv\+\_\+ring\+\_\+len)
\begin{DoxyCompactList}\small\item\em 使用已存在的套接字创建管道 \end{DoxyCompactList}\item 
int \hyperlink{a00117_ga37ab03493fb1ed037815f2cd951d762a_ga37ab03493fb1ed037815f2cd951d762a}{knet\+\_\+loop\+\_\+run\+\_\+once} (\hyperlink{a00056_a97fc76209a58362019f1ded9169e397f_a97fc76209a58362019f1ded9169e397f}{kloop\+\_\+t} $\ast$loop)
\begin{DoxyCompactList}\small\item\em 运行一次事件循环 kloop\+\_\+t不是线程安全的，不能在多个线程内同时对同一个kloop\+\_\+t实例调用knet\+\_\+loop\+\_\+run\+\_\+once \end{DoxyCompactList}\item 
int \hyperlink{a00117_gaaf104ae1337c32415bd1aa8df545ae31_gaaf104ae1337c32415bd1aa8df545ae31}{knet\+\_\+loop\+\_\+run} (\hyperlink{a00056_a97fc76209a58362019f1ded9169e397f_a97fc76209a58362019f1ded9169e397f}{kloop\+\_\+t} $\ast$loop)
\begin{DoxyCompactList}\small\item\em 运行事件循环直到调用knet\+\_\+loop\+\_\+exit() kloop\+\_\+t不是线程安全的，不能在多个线程内同时对同一个kloop\+\_\+t实例调用knet\+\_\+loop\+\_\+run \end{DoxyCompactList}\item 
void \hyperlink{a00117_ga9e04a2d06c79405f444aa53a26eedd90_ga9e04a2d06c79405f444aa53a26eedd90}{knet\+\_\+loop\+\_\+exit} (\hyperlink{a00056_a97fc76209a58362019f1ded9169e397f_a97fc76209a58362019f1ded9169e397f}{kloop\+\_\+t} $\ast$loop)
\begin{DoxyCompactList}\small\item\em 退出函数knet\+\_\+loop\+\_\+run() \end{DoxyCompactList}\item 
int \hyperlink{a00117_ga1eca6d310350a4545182666db28fb91f_ga1eca6d310350a4545182666db28fb91f}{knet\+\_\+loop\+\_\+get\+\_\+active\+\_\+channel\+\_\+count} (\hyperlink{a00056_a97fc76209a58362019f1ded9169e397f_a97fc76209a58362019f1ded9169e397f}{kloop\+\_\+t} $\ast$loop)
\begin{DoxyCompactList}\small\item\em 获取活跃管道数量 \end{DoxyCompactList}\item 
int \hyperlink{a00117_ga8a8c7aee4eed09d50062a5f464cdba7a_ga8a8c7aee4eed09d50062a5f464cdba7a}{knet\+\_\+loop\+\_\+get\+\_\+close\+\_\+channel\+\_\+count} (\hyperlink{a00056_a97fc76209a58362019f1ded9169e397f_a97fc76209a58362019f1ded9169e397f}{kloop\+\_\+t} $\ast$loop)
\begin{DoxyCompactList}\small\item\em 获取已关闭管道数量 \end{DoxyCompactList}\item 
void \hyperlink{a00117_gaf1239ea65307d7dc588175c45947437b_gaf1239ea65307d7dc588175c45947437b}{knet\+\_\+loop\+\_\+set\+\_\+data} (\hyperlink{a00056_a97fc76209a58362019f1ded9169e397f_a97fc76209a58362019f1ded9169e397f}{kloop\+\_\+t} $\ast$loop, void $\ast$data)
\begin{DoxyCompactList}\small\item\em 设置用户数据指针 \end{DoxyCompactList}\item 
\hyperlink{a00056_ab75a5c23099a6118c469ed160b277f28_ab75a5c23099a6118c469ed160b277f28}{kloop\+\_\+profile\+\_\+t} $\ast$ \hyperlink{a00117_gafc6a946f7c7fd4fb0508ac0f88969da1_gafc6a946f7c7fd4fb0508ac0f88969da1}{knet\+\_\+loop\+\_\+get\+\_\+profile} (\hyperlink{a00056_a97fc76209a58362019f1ded9169e397f_a97fc76209a58362019f1ded9169e397f}{kloop\+\_\+t} $\ast$loop)
\begin{DoxyCompactList}\small\item\em 取得统计器 \end{DoxyCompactList}\end{DoxyCompactItemize}


\subsection{详细描述}
网络事件循环 


\begin{DoxyPre}
网络事件API，作为各个不同操作系统网络选取器的包装，屏蔽了不同平台的具体实现，
为你提供统一的调用接口.\end{DoxyPre}



\begin{DoxyPre}管道引用kchannel\_ref\_t通过调用knet\_loop\_create\_channel和knet\_loop\_create\_channel\_exist\_socket\_fd
创建，knet\_loop\_run将启动事件循环并等待调用knet\_loop\_exit退出，你可以手动调用knet\_loop\_run\_once运行一次事件
循环自己控制循环的调用频率.\end{DoxyPre}



\begin{DoxyPre}每个kloop\_t内都维护了活跃管道和已关闭(未销毁)管道的双向链表，可以通过knet\_loop\_get\_active\_channel\_count
和knet\_loop\_get\_close\_channel\_count来取得具体数量.\end{DoxyPre}



\begin{DoxyPre}在创建管道时，要注意两个重要的配置参数：\end{DoxyPre}



\begin{DoxyPre}1. max\_send\_list\_len 发送链表最大元素个数
2. recv\_ring\_len     接受缓冲区最大长度\end{DoxyPre}



\begin{DoxyPre}通常向管道内发送数据(stream\_push\_系列)，管道会尝试直接发送，并不缓存要发送的数据，如果因为某种原因
导致不能直接发送，数据会被缓存在发送链表内等待合适的时机发送，如果发送链表的长度达到上限，管道会被关闭.
同样，接受缓冲区会从套接字内将数据读取出来，如果你一直不从kstream\_t内取数据，那么早晚会被写满，管道也
会被关闭.\end{DoxyPre}



\begin{DoxyPre}\end{DoxyPre}
 

\subsection{函数说明}
\hypertarget{a00117_ga2e4181ebe9c1769be90bb8dc0e5cdffe_ga2e4181ebe9c1769be90bb8dc0e5cdffe}{}\index{事件循环@{事件循环}!knet\+\_\+loop\+\_\+create@{knet\+\_\+loop\+\_\+create}}
\index{knet\+\_\+loop\+\_\+create@{knet\+\_\+loop\+\_\+create}!事件循环@{事件循环}}
\subsubsection[{knet\+\_\+loop\+\_\+create}]{\setlength{\rightskip}{0pt plus 5cm}{\bf kloop\+\_\+t}$\ast$ knet\+\_\+loop\+\_\+create (
\begin{DoxyParamCaption}
{}
\end{DoxyParamCaption}
)}\label{a00117_ga2e4181ebe9c1769be90bb8dc0e5cdffe_ga2e4181ebe9c1769be90bb8dc0e5cdffe}


创建一个事件循环 

\begin{DoxyReturn}{返回}
kloop\+\_\+t实例 
\end{DoxyReturn}
\hypertarget{a00117_gac1f9a4848c06c2a6a2723d7b991b4394_gac1f9a4848c06c2a6a2723d7b991b4394}{}\index{事件循环@{事件循环}!knet\+\_\+loop\+\_\+create\+\_\+channel@{knet\+\_\+loop\+\_\+create\+\_\+channel}}
\index{knet\+\_\+loop\+\_\+create\+\_\+channel@{knet\+\_\+loop\+\_\+create\+\_\+channel}!事件循环@{事件循环}}
\subsubsection[{knet\+\_\+loop\+\_\+create\+\_\+channel}]{\setlength{\rightskip}{0pt plus 5cm}{\bf kchannel\+\_\+ref\+\_\+t}$\ast$ knet\+\_\+loop\+\_\+create\+\_\+channel (
\begin{DoxyParamCaption}
\item[{{\bf kloop\+\_\+t} $\ast$}]{loop, }
\item[{uint32\+\_\+t}]{max\+\_\+send\+\_\+list\+\_\+len, }
\item[{uint32\+\_\+t}]{recv\+\_\+ring\+\_\+len}
\end{DoxyParamCaption}
)}\label{a00117_gac1f9a4848c06c2a6a2723d7b991b4394_gac1f9a4848c06c2a6a2723d7b991b4394}


创建管道 


\begin{DoxyParams}{参数}
{\em loop} & kloop\+\_\+t实例 \\
\hline
{\em max\+\_\+send\+\_\+list\+\_\+len} & 发送缓冲区链最大长度 \\
\hline
{\em recv\+\_\+ring\+\_\+len} & 接受环形缓冲区最大长度 \\
\hline
\end{DoxyParams}
\begin{DoxyReturn}{返回}
kchannel\+\_\+ref\+\_\+t实例 
\end{DoxyReturn}
\hypertarget{a00117_gaddfd5a2709d0c26ada8a2dd3fd6a5b62_gaddfd5a2709d0c26ada8a2dd3fd6a5b62}{}\index{事件循环@{事件循环}!knet\+\_\+loop\+\_\+create\+\_\+channel\+\_\+exist\+\_\+socket\+\_\+fd@{knet\+\_\+loop\+\_\+create\+\_\+channel\+\_\+exist\+\_\+socket\+\_\+fd}}
\index{knet\+\_\+loop\+\_\+create\+\_\+channel\+\_\+exist\+\_\+socket\+\_\+fd@{knet\+\_\+loop\+\_\+create\+\_\+channel\+\_\+exist\+\_\+socket\+\_\+fd}!事件循环@{事件循环}}
\subsubsection[{knet\+\_\+loop\+\_\+create\+\_\+channel\+\_\+exist\+\_\+socket\+\_\+fd}]{\setlength{\rightskip}{0pt plus 5cm}{\bf kchannel\+\_\+ref\+\_\+t}$\ast$ knet\+\_\+loop\+\_\+create\+\_\+channel\+\_\+exist\+\_\+socket\+\_\+fd (
\begin{DoxyParamCaption}
\item[{{\bf kloop\+\_\+t} $\ast$}]{loop, }
\item[{{\bf socket\+\_\+t}}]{socket\+\_\+fd, }
\item[{uint32\+\_\+t}]{max\+\_\+send\+\_\+list\+\_\+len, }
\item[{uint32\+\_\+t}]{recv\+\_\+ring\+\_\+len}
\end{DoxyParamCaption}
)}\label{a00117_gaddfd5a2709d0c26ada8a2dd3fd6a5b62_gaddfd5a2709d0c26ada8a2dd3fd6a5b62}


使用已存在的套接字创建管道 


\begin{DoxyParams}{参数}
{\em loop} & kloop\+\_\+t实例 \\
\hline
{\em socket\+\_\+fd} & 套接字 \\
\hline
{\em max\+\_\+send\+\_\+list\+\_\+len} & 发送缓冲区链最大长度 \\
\hline
{\em recv\+\_\+ring\+\_\+len} & 接受环形缓冲区最大长度 \\
\hline
\end{DoxyParams}
\begin{DoxyReturn}{返回}
kchannel\+\_\+ref\+\_\+t实例 
\end{DoxyReturn}
\hypertarget{a00117_ga390ea3161f935d0d3a411df8752d8b9f_ga390ea3161f935d0d3a411df8752d8b9f}{}\index{事件循环@{事件循环}!knet\+\_\+loop\+\_\+destroy@{knet\+\_\+loop\+\_\+destroy}}
\index{knet\+\_\+loop\+\_\+destroy@{knet\+\_\+loop\+\_\+destroy}!事件循环@{事件循环}}
\subsubsection[{knet\+\_\+loop\+\_\+destroy}]{\setlength{\rightskip}{0pt plus 5cm}void knet\+\_\+loop\+\_\+destroy (
\begin{DoxyParamCaption}
\item[{{\bf kloop\+\_\+t} $\ast$}]{loop}
\end{DoxyParamCaption}
)}\label{a00117_ga390ea3161f935d0d3a411df8752d8b9f_ga390ea3161f935d0d3a411df8752d8b9f}


销毁事件循环 事件循环内的所有管道也会被销毁 


\begin{DoxyParams}{参数}
{\em loop} & kloop\+\_\+t实例 \\
\hline
\end{DoxyParams}
\hypertarget{a00117_ga9e04a2d06c79405f444aa53a26eedd90_ga9e04a2d06c79405f444aa53a26eedd90}{}\index{事件循环@{事件循环}!knet\+\_\+loop\+\_\+exit@{knet\+\_\+loop\+\_\+exit}}
\index{knet\+\_\+loop\+\_\+exit@{knet\+\_\+loop\+\_\+exit}!事件循环@{事件循环}}
\subsubsection[{knet\+\_\+loop\+\_\+exit}]{\setlength{\rightskip}{0pt plus 5cm}void knet\+\_\+loop\+\_\+exit (
\begin{DoxyParamCaption}
\item[{{\bf kloop\+\_\+t} $\ast$}]{loop}
\end{DoxyParamCaption}
)}\label{a00117_ga9e04a2d06c79405f444aa53a26eedd90_ga9e04a2d06c79405f444aa53a26eedd90}


退出函数knet\+\_\+loop\+\_\+run() 


\begin{DoxyParams}{参数}
{\em loop} & kloop\+\_\+t实例 \\
\hline
\end{DoxyParams}
\hypertarget{a00117_ga1eca6d310350a4545182666db28fb91f_ga1eca6d310350a4545182666db28fb91f}{}\index{事件循环@{事件循环}!knet\+\_\+loop\+\_\+get\+\_\+active\+\_\+channel\+\_\+count@{knet\+\_\+loop\+\_\+get\+\_\+active\+\_\+channel\+\_\+count}}
\index{knet\+\_\+loop\+\_\+get\+\_\+active\+\_\+channel\+\_\+count@{knet\+\_\+loop\+\_\+get\+\_\+active\+\_\+channel\+\_\+count}!事件循环@{事件循环}}
\subsubsection[{knet\+\_\+loop\+\_\+get\+\_\+active\+\_\+channel\+\_\+count}]{\setlength{\rightskip}{0pt plus 5cm}int knet\+\_\+loop\+\_\+get\+\_\+active\+\_\+channel\+\_\+count (
\begin{DoxyParamCaption}
\item[{{\bf kloop\+\_\+t} $\ast$}]{loop}
\end{DoxyParamCaption}
)}\label{a00117_ga1eca6d310350a4545182666db28fb91f_ga1eca6d310350a4545182666db28fb91f}


获取活跃管道数量 


\begin{DoxyParams}{参数}
{\em loop} & kloop\+\_\+t实例 \\
\hline
\end{DoxyParams}
\begin{DoxyReturn}{返回}
活跃管道数量 
\end{DoxyReturn}
\hypertarget{a00117_ga8a8c7aee4eed09d50062a5f464cdba7a_ga8a8c7aee4eed09d50062a5f464cdba7a}{}\index{事件循环@{事件循环}!knet\+\_\+loop\+\_\+get\+\_\+close\+\_\+channel\+\_\+count@{knet\+\_\+loop\+\_\+get\+\_\+close\+\_\+channel\+\_\+count}}
\index{knet\+\_\+loop\+\_\+get\+\_\+close\+\_\+channel\+\_\+count@{knet\+\_\+loop\+\_\+get\+\_\+close\+\_\+channel\+\_\+count}!事件循环@{事件循环}}
\subsubsection[{knet\+\_\+loop\+\_\+get\+\_\+close\+\_\+channel\+\_\+count}]{\setlength{\rightskip}{0pt plus 5cm}int knet\+\_\+loop\+\_\+get\+\_\+close\+\_\+channel\+\_\+count (
\begin{DoxyParamCaption}
\item[{{\bf kloop\+\_\+t} $\ast$}]{loop}
\end{DoxyParamCaption}
)}\label{a00117_ga8a8c7aee4eed09d50062a5f464cdba7a_ga8a8c7aee4eed09d50062a5f464cdba7a}


获取已关闭管道数量 


\begin{DoxyParams}{参数}
{\em loop} & kloop\+\_\+t实例 \\
\hline
\end{DoxyParams}
\begin{DoxyReturn}{返回}
关闭管道数量 
\end{DoxyReturn}
\hypertarget{a00117_gafc6a946f7c7fd4fb0508ac0f88969da1_gafc6a946f7c7fd4fb0508ac0f88969da1}{}\index{事件循环@{事件循环}!knet\+\_\+loop\+\_\+get\+\_\+profile@{knet\+\_\+loop\+\_\+get\+\_\+profile}}
\index{knet\+\_\+loop\+\_\+get\+\_\+profile@{knet\+\_\+loop\+\_\+get\+\_\+profile}!事件循环@{事件循环}}
\subsubsection[{knet\+\_\+loop\+\_\+get\+\_\+profile}]{\setlength{\rightskip}{0pt plus 5cm}{\bf kloop\+\_\+profile\+\_\+t}$\ast$ knet\+\_\+loop\+\_\+get\+\_\+profile (
\begin{DoxyParamCaption}
\item[{{\bf kloop\+\_\+t} $\ast$}]{loop}
\end{DoxyParamCaption}
)}\label{a00117_gafc6a946f7c7fd4fb0508ac0f88969da1_gafc6a946f7c7fd4fb0508ac0f88969da1}


取得统计器 


\begin{DoxyParams}{参数}
{\em loop} & kloop\+\_\+t实例 \\
\hline
\end{DoxyParams}
\begin{DoxyReturn}{返回}
kloop\+\_\+profile\+\_\+t实例 
\end{DoxyReturn}
\hypertarget{a00117_gaaf104ae1337c32415bd1aa8df545ae31_gaaf104ae1337c32415bd1aa8df545ae31}{}\index{事件循环@{事件循环}!knet\+\_\+loop\+\_\+run@{knet\+\_\+loop\+\_\+run}}
\index{knet\+\_\+loop\+\_\+run@{knet\+\_\+loop\+\_\+run}!事件循环@{事件循环}}
\subsubsection[{knet\+\_\+loop\+\_\+run}]{\setlength{\rightskip}{0pt plus 5cm}int knet\+\_\+loop\+\_\+run (
\begin{DoxyParamCaption}
\item[{{\bf kloop\+\_\+t} $\ast$}]{loop}
\end{DoxyParamCaption}
)}\label{a00117_gaaf104ae1337c32415bd1aa8df545ae31_gaaf104ae1337c32415bd1aa8df545ae31}


运行事件循环直到调用knet\+\_\+loop\+\_\+exit() kloop\+\_\+t不是线程安全的，不能在多个线程内同时对同一个kloop\+\_\+t实例调用knet\+\_\+loop\+\_\+run 


\begin{DoxyParams}{参数}
{\em loop} & kloop\+\_\+t实例 \\
\hline
\end{DoxyParams}

\begin{DoxyRetVals}{返回值}
{\em error\+\_\+ok} & 成功 \\
\hline
{\em 其他} & 失败 \\
\hline
\end{DoxyRetVals}
\hypertarget{a00117_ga37ab03493fb1ed037815f2cd951d762a_ga37ab03493fb1ed037815f2cd951d762a}{}\index{事件循环@{事件循环}!knet\+\_\+loop\+\_\+run\+\_\+once@{knet\+\_\+loop\+\_\+run\+\_\+once}}
\index{knet\+\_\+loop\+\_\+run\+\_\+once@{knet\+\_\+loop\+\_\+run\+\_\+once}!事件循环@{事件循环}}
\subsubsection[{knet\+\_\+loop\+\_\+run\+\_\+once}]{\setlength{\rightskip}{0pt plus 5cm}int knet\+\_\+loop\+\_\+run\+\_\+once (
\begin{DoxyParamCaption}
\item[{{\bf kloop\+\_\+t} $\ast$}]{loop}
\end{DoxyParamCaption}
)}\label{a00117_ga37ab03493fb1ed037815f2cd951d762a_ga37ab03493fb1ed037815f2cd951d762a}


运行一次事件循环 kloop\+\_\+t不是线程安全的，不能在多个线程内同时对同一个kloop\+\_\+t实例调用knet\+\_\+loop\+\_\+run\+\_\+once 


\begin{DoxyParams}{参数}
{\em loop} & kloop\+\_\+t实例 \\
\hline
\end{DoxyParams}

\begin{DoxyRetVals}{返回值}
{\em error\+\_\+ok} & 成功 \\
\hline
{\em 其他} & 失败 \\
\hline
\end{DoxyRetVals}
\hypertarget{a00117_gaf1239ea65307d7dc588175c45947437b_gaf1239ea65307d7dc588175c45947437b}{}\index{事件循环@{事件循环}!knet\+\_\+loop\+\_\+set\+\_\+data@{knet\+\_\+loop\+\_\+set\+\_\+data}}
\index{knet\+\_\+loop\+\_\+set\+\_\+data@{knet\+\_\+loop\+\_\+set\+\_\+data}!事件循环@{事件循环}}
\subsubsection[{knet\+\_\+loop\+\_\+set\+\_\+data}]{\setlength{\rightskip}{0pt plus 5cm}void knet\+\_\+loop\+\_\+set\+\_\+data (
\begin{DoxyParamCaption}
\item[{{\bf kloop\+\_\+t} $\ast$}]{loop, }
\item[{void $\ast$}]{data}
\end{DoxyParamCaption}
)}\label{a00117_gaf1239ea65307d7dc588175c45947437b_gaf1239ea65307d7dc588175c45947437b}


设置用户数据指针 


\begin{DoxyParams}{参数}
{\em loop} & kloop\+\_\+t实例 \\
\hline
{\em data} & 用户数据指针 \\
\hline
\end{DoxyParams}
